package com.vgemv.jsutilitysdkdemo.module.TextView;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.vgemv.jsutilitysdk.geometry.JsGeometryManager;
import com.vgemv.jsutilitysdk.JsTextView.JsTextView;
import com.vgemv.jsutilitysdk.Menu.JsMenuFragment.JsMenuFragment.JsMenuFragment;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemStyleDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemView.JsMenuItemView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVm;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuItemVm.JsMenuItemVmDefault;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuView;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewAdapter;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewBasicCallback;
import com.vgemv.jsutilitysdk.Menu.JsMenuView.JsMenuViewVm;

import java.util.ArrayList;

public class JsTextViewActivity extends AppCompatActivity {


    public Handler mHandler = new Handler(Looper.getMainLooper());

    private JsTextView mtv;
    private RelativeLayout mrl_root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);



        RelativeLayout relativeLayout = new RelativeLayout(this);
        relativeLayout.setId(View.generateViewId());
        mrl_root = relativeLayout;
        setContentView(relativeLayout);


        FrameLayout fl = new FrameLayout(this);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,JsGeometryManager.pixelFromDp(300));
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        relativeLayout.addView(fl,lp);
        JsMenuFragment menuFragment = new JsMenuFragment();
        menuFragment.moveToParentView(this,fl);

        final int itemSize = JsGeometryManager.pixelFromDp(80);

        menuFragment.mBasicCallback = new JsMenuViewBasicCallback() {
            @Override
            public JsMenuViewAdapter jsMenuViewSetAdapter(JsMenuView menuView) {
                return null;
            }

            @Override
            public void jsMenuItemViewWillDisplay(RecyclerView.ViewHolder viewHolder, int position) {

            }

            @Override
            public Rect jsMenuViewSetItemOffset(JsMenuView view, int index) {
                int spanCount = view.mMenuViewVm.mi_spanCount;
                int hPadding = (JsGeometryManager.getScreenSizeInPixel().getWidth() - spanCount * itemSize)/(spanCount + 2);
                int vPadding = JsGeometryManager.pixelFromDp(20);

                Rect rect = new Rect();
                if(index == 0) {
                    rect.top = vPadding;
                    rect.left = hPadding;
                    rect.right = hPadding;
                    rect.bottom = vPadding;
                }
                else if(index == view.vms().size() - 1) {
                    rect.top = vPadding;
                    rect.left = hPadding;
                    rect.right = hPadding;
                    rect.bottom = vPadding;
                }
                else {
                    rect.top = vPadding;
                    rect.left = hPadding;
                    rect.right = hPadding;
                    rect.bottom = vPadding;
                }
                return rect;

            }

            @Override
            public JsMenuViewVm jsMenuViewSetVm(JsMenuView f) {
                JsMenuViewVm vm = new JsMenuViewVm();
                vm.mi_spanCount = 4;
                return vm;
            }

            @Override
            public JsMenuItemView jsMenuViewCreateItemView(JsMenuView f, Context context) {
                return null;
            }

            @Override
            public void jsMenuViewAfterCreateView(JsMenuView fragment) {

            }

            @Override
            public ArrayList<JsMenuItemVm> jsMenuViewSetDatasource() {
                ArrayList<JsMenuItemVm> models = new ArrayList<JsMenuItemVm>();

                JsMenuItemStyleDefault style = new JsMenuItemStyleDefault();
                style.mI_backgroundColor = Color.BLACK;
                style.mI_titleTextColor = Color.WHITE;
                style.mi_height = itemSize;
                style.mi_width = itemSize;

                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(0,"Short","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(1,"Medium","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
                {
                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(2,"Long","",itemSize,itemSize);
                    vm.setStyle(style);
                    models.add(vm);
                }
//                {
//                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(3,"Warn","",itemSize,itemSize);
//                    vm.setStyle(style);
//                    models.add(vm);
//                }
//                {
//                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(4,"Error","",itemSize,itemSize);
//                    vm.setStyle(style);
//                    models.add(vm);
//                }
//                {
//                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(5,"Success","",itemSize,itemSize);
//                    vm.setStyle(style);
//                    models.add(vm);
//                }
//                {
//                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(6,"ForceUndisplayReminder","",itemSize,itemSize);
//                    vm.setStyle(style);
//                    models.add(vm);
//                }
//                {
//                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(7,"A long text","",itemSize,itemSize);
//                    vm.setStyle(style);
//                    models.add(vm);
//                }
//                {
//                    JsMenuItemVmDefault vm =  new JsMenuItemVmDefault(8,"A short text","",itemSize,itemSize);
//                    vm.setStyle(style);
//                    models.add(vm);
//                }
                return models;
            }

            @Override
            public void jsMenuViewClickItem(JsMenuView fragmentAlpha, int i, JsMenuItemVm model) {
                switch (i) {
                    case 0:{
                        showMessage("Hello");
                        break;
                    }
                    case 1:{
                       showMessage("This is a Media Length message");
                        break;
                    }
                    case 2: {
                        showMessage("This is a long long long long long long long long long long long long long long long long message.");
                       break;
                    }
//                    case 3: {
//                        //warn
//                        JsActivityContentDisplayManager.sharedInstance().displayMessage("This is warn", JsTextView.this, JsReminderManager.JsMessageReminderType.Warn);
//                        break;
//                    }
//                    case 4: {
//                        //error
//                        JsActivityContentDisplayManager.sharedInstance().displayMessage("This is error", JsTextView.this, JsReminderManager.JsMessageReminderType.Error);
//                        break;
//                    }
//                    case 5 : {
//                        //success
//                        JsActivityContentDisplayManager.sharedInstance().displayMessage("This is success", JsTextView.this, JsReminderManager.JsMessageReminderType.Success);
//                        break;
//                    }
//                    case 6: {
//                        //ForceDisunplay
//                        JsActivityContentDisplayManager.sharedInstance().undisplayLoadingReminderForce(JsTextView.this);
//                        break;
//                    }
//                    case 7: {
//                        //ForceDisunplay
//                        JsActivityContentDisplayManager.sharedInstance().displayMessage("This is a long long long long long long long long long long long long long long  success message", JsTextView.this);
//                        break;
//                    }
//                    case 8: {
//                        //ForceDisunplay
//                        JsActivityContentDisplayManager.sharedInstance().displayMessage("Hello", JsTextView.this);
//                        break;
//                    }
                    default:{
                        break;
                    }
                }
            }
        };
    }

    public void showMessage(String message ) {
        if(mtv != null) {
            //获取最底层的view DecoverView.
            ViewGroup viewGroup =(ViewGroup)mtv.getRootView();
            if(viewGroup != null) {
                mrl_root.removeView(mtv);
            }
        }
        mtv = new JsTextView(JsTextViewActivity.this);
        mtv.setBackgroundColor(Color.RED);
        mtv.setMaxWidth(800);
        mtv.setText(message);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mrl_root.addView(mtv);
    }
}

